package com.wyp168.socket.QQ.qqclient.services;

import com.wyp168.socket.QQ.qqclient.common.Message;
import com.wyp168.socket.QQ.qqclient.common.User;
import com.wyp168.socket.QQ.qqclient.common.constant.MessageType;
import com.wyp168.socket.QQ.qqclient.services.ClientConnectServerThread;
import com.wyp168.socket.QQ.qqclient.services.ManagerClientConnectServerThread;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @ClassName UserClientService
 * @Title 和用户相关的方法(登录、登出等)
 * @Author wyp168
 * @Date 2022/10/30 01:17
 */
public class UserClientService {

    private static User user = new User();

    private static Socket socket;


    /**
     * 通过socket将用户名和密码发送给服务端，判断账号密码是否正确
     * @param username 用户名
     * @param password 密码
     * @return 用户名密码是否合法/正确
     */
    public static boolean userLogin(String username, String password){
        boolean b = false;
        try {
            socket = new Socket(InetAddress.getLocalHost(), 9999);   //连接到服务器的9999端口，并发送账号密码到服务端，进行验证

            user = new User(username, password);
            OutputStream outputStream = socket.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outputStream);
            oos.writeObject(user);  //发送User对象

            //读取服务端回复的Message对象
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            Message message = (Message) ois.readObject();

            if (message.getMessageType().equals("1")) { //账号密码正确，登录成功
                //创建一个和服务器端保持通信的线程 ---> 创建一个Socket ClientConnectServerThread
                ClientConnectServerThread clientConnectServerThread =
                        new ClientConnectServerThread(user.getUserId(), socket);
                clientConnectServerThread.setDaemon(true);
                //启动客户端线程
                clientConnectServerThread.start();
                //为了客户端扩展，现将当线程对象clientConnectServerThread 放入到线程集合中
                ManagerClientConnectServerThread.addClientConnectServerThread(user.getUserId(), clientConnectServerThread);

                user.setUserId(username);
                user.setPassword(password);

                b = true;
            } else {
                //如果登录失败，则不能启动和服务器通信的线程，所以，得关闭socket
                socket.close();
            }
        } catch (ClassNotFoundException | IOException e) {
            e.printStackTrace();
        }


        return b;
    }

    /**
     * 客户端请求服务端获取所有用户列表
     */
    public static void getOnlineUsers() {
        try {
            Message message = new Message();
            message.setMessageType(MessageType.MESSAGE_GET_ONLINE_USERS);
            message.setSender(user.getUserId());

            //从客户端管理线程的集合中，通过userId得到当前线程的socket对象
//            ObjectOutputStream objectOutputStream =
//                    new ObjectOutputStream(ManagerClientConnectServerThread.getClientConnectServerThread(user.getUserId()).getSocket().getOutputStream());
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 客户端向客户端请求登出/注销
     */
    public static void logoutUser() {
        ObjectOutputStream oos = null;
        try {
            Message message = new Message();
            message.setMessageType(MessageType.MESSAGE_CLIENT_EXIT);
            message.setSender(user.getUserId());    //指定是哪个客户端id，用户服务器集合辨别

            oos = new ObjectOutputStream(socket.getOutputStream());
            System.out.println(message.toString());
            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
